package xiangya.bysj.service.impl;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.ImMsg;
import com.taobao.api.domain.Userinfos;
import com.taobao.api.request.OpenimImmsgPushRequest;
import com.taobao.api.request.OpenimUsersAddRequest;
import com.taobao.api.request.OpenimUsersDeleteRequest;
import com.taobao.api.request.OpenimUsersGetRequest;
import com.taobao.api.request.OpenimUsersUpdateRequest;
import com.taobao.api.response.OpenimImmsgPushResponse;
import com.taobao.api.response.OpenimUsersAddResponse;
import com.taobao.api.response.OpenimUsersDeleteResponse;
import com.taobao.api.response.OpenimUsersGetResponse;
import com.taobao.api.response.OpenimUsersUpdateResponse;

import xiangya.bysj.service.interf.IMServiceInterface;

/**
 * IM的业务类
 * 
 * @author Mr.zhang
 *
 */
@Service("IMService")
public class IMServiceImpl implements IMServiceInterface {

	private List<Userinfos> list;
	/**
	 * 增加用户的请求方法
	 */
	private OpenimUsersAddRequest imAddReq;
	/**
	 * 增加用户的响应方法
	 */
	private OpenimUsersAddResponse imAddRes;
	/**
	 * 推送消息的请求方法
	 */
	private OpenimImmsgPushRequest imMsgReq;
	/**
	 * 推送消息的响应方法
	 */
	private OpenimImmsgPushResponse imMsgRes;
	/**
	 * 删除用户的请求方法
	 */
	private OpenimUsersDeleteRequest imDelReq;
	/**
	 * 删除用户的响应方法
	 */
	private OpenimUsersDeleteResponse imDelRes;
	/**
	 * 更新用户的请求方法
	 */
	private OpenimUsersUpdateRequest imUpReq;
	/**
	 * 更新用户的响应方法
	 */
	private OpenimUsersUpdateResponse imUpRes;
	/**
	 * 获取用户列表的请求方法
	 */
	private OpenimUsersGetRequest imGetReq;
	/**
	 * 获取用户列表的响应方法
	 */
	private OpenimUsersGetResponse imGetRes;

	private final static String RES_ERROR = "error_response";

	@Resource(name = "aliClient")
	private TaobaoClient imClient;

	/**
	 * 在百川上面注册账号
	 */
	@Override
	public void save(Userinfos obj) throws Exception {
		list = new ArrayList<Userinfos>();
		list.add(obj);
		// 阿里百川提供的增加用户的方法
		imAddReq = new OpenimUsersAddRequest();
		imAddReq.setUserinfos(list);
		imAddRes = imClient.execute(imAddReq);
		if (!imAddRes.isSuccess() || imAddRes.getBody().contains(RES_ERROR) || imAddRes.getUidSucc().isEmpty()) {
			throw new RuntimeException();
		}
	}

	/**
	 * 更新
	 */
	@Override
	public void update(Userinfos obj) throws Exception {
		List<Userinfos> list = new ArrayList<Userinfos>();
		list.add(obj);
		imUpReq = new OpenimUsersUpdateRequest();
		imUpReq.setUserinfos(list);
		imUpRes = imClient.execute(imUpReq);
		if (!imUpRes.isSuccess() || imUpRes.getBody().contains("RES_ERROR") || imUpRes.getUidSucc().isEmpty()) {
			throw new RuntimeException();
		}
	}

	/**
	 * 删除
	 */
	@Override
	public void delete(int objId) throws Exception {

	}

	/**
	 * 推送消息
	 */
	@Override
	public void sendMsg(ImMsg imMsg) throws Exception {
		imMsgReq = new OpenimImmsgPushRequest();
		imMsgReq.setImmsg(imMsg);
		imMsgRes = imClient.execute(imMsgReq);
		if (!imMsgRes.isSuccess() || imMsgRes.getBody().contains(RES_ERROR)) {
			throw new RuntimeException();
		}
	}

	@Override
	public void deleteByCode(String userCode) throws Exception {
		imDelReq = new OpenimUsersDeleteRequest();
		imDelReq.setUserids(userCode);
		imDelRes = imClient.execute(imDelReq);
		if (!imDelRes.isSuccess() || imDelRes.getBody().contains("error_response")) {
			throw new RuntimeException();
		}
	}

	@Override
	public List<Userinfos> getSome(String[] userCode) throws Exception {
		List<Userinfos> users = new ArrayList<Userinfos>();
		imGetReq = new OpenimUsersGetRequest();
		for (int i = 0; i < userCode.length; i++) {
			imGetReq.setUserids(userCode[i]);
			imGetRes = imClient.execute(imGetReq);
			List<Userinfos> list = imGetRes.getUserinfos();
			if (list != null )
				for (int j = 0; j < list.size(); j++) {
					users.add(list.get(j));
				}
		}
		return users;
	}
}
